<?php include('fns_db_forum.php'); ?>
<?php include('../views/layouts/_header.php'); ?>
<?php include('../views/layouts/_left.php'); ?>
<?php if(!checkIfAdmin()){
		header("location: forum.php");
	} ?>

<?php
if (isset($_GET['action']))
{
	$action = $_GET['action'];
	if ($action == '')
	{
		unset($action);
	}
}

$user = $_SESSION['user'];
$author = $user->user_name;

switch ($action){
	case 'addForum':
		if (isset($_POST['name']))
		{
			$name = $_POST['name'];

			if ($name == '')
			{
				unset($name);
			}
		}

		if (isset($name))
		{
			$result = insert_forum($name, $author);

			if ($result == 'true')
			{
				echo "<p>Forum added successfully!</p>";
			}
			else
			{
				echo "<p>Forum not added!</p>";
			}
		}
		else
		{
			echo "<p>Not all fields are filled. Forum cannot be added.</p>";
		}

		break;


	case 'addTopic':
		if (isset($_POST['id']))
		{
			$id = $_POST['id'];
		}
		if (isset($_POST['name']))
		{
			$name = $_POST['name'];

			if ($name == '')
			{
				unset($name);
			}
		}

		if (isset($name) && isset($id))
		{
			$result = insert_topic($id, $name, $author);

			if ($result == 'true')
			{
				echo '<meta http-equiv="Refresh" content="2;';
				echo 'url=forum.php"/>';
				echo "<B>Topic added successfully! In a moment
				you will be
				automatically returned to the forums page.</b>";
			}
			else
			{
				echo "<p>Topic not added!</p>";
			}
		}
		else
		{
			echo "<p>Not all fields are filled. Forum cannot be added.</p>";
		}

		break;


	default:
		echo "<p>Unexpected parameter passed!</p>";
}
?>

<?php include('../views/layouts/_footer.php'); ?>